﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DatNuocViet.Models
{
    public class NewsDAO
    {
        public static List<New> GetListNews(int _type, int _limit)
        {
            try
            {
                DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

                List<New> listNews = (from c in data.News
                                      where c.Deleted == false && c.NewsType == _type
                                      orderby c.CreateOn descending
                                      select c).Take(_limit).ToList();

                return listNews;
            }
            catch
            {
                return new List<New>();
            }
        }

        public static New GetLastNews(int _type)
        {
            try
            {
                DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

                New News = (from c in data.News
                            where c.Deleted == false && c.NewsType == _type
                            orderby c.CreateOn descending
                            select c).First();

                return News;
            }
            catch
            {
                return new New();
            }
        }

        public static List<NewsType> GetListNewsType()
        {
            try
            {
                DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

                List<NewsType> listNesType = (from c in data.NewsTypes
                                              where c.Deleted == false
                                              orderby c.SortOrder ascending
                                              select c).ToList();

                return listNesType;
            }
            catch
            {
                return new List<NewsType>();
            }
        }

        public static NewsType GetNewsType(int _type)
        {
            DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

            NewsType result = data.NewsTypes.Single(p => p.Id == _type);

            return result;
        }

        public static List<GetNewsTypeResult> GetNewsType()
        {
            DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

            List<GetNewsTypeResult> result = data.GetNewsType().ToList();

            return result;
        }

        public static New GetNewsDetails(int _id)
        {
            try
            {
                DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

                New result = data.News.Single(p => p.Id == _id);

                return result;
            }
            catch
            {
                return new New();
            }
        }

        public static int getAmountOfNewsByType(int _type)
        {
            DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

            return (data.DemDanhSachTin(_type.ToString(), 1, null) ?? 0);
        }

        public static List<New> GetNewsByType(int _pageindex, int _pagesize, int _type)
        {
            try
            {
                DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

                List<LayDanhSachTinResult> listNews = data.LayDanhSachTin(_pageindex, _pagesize, null, _type.ToString(), 1, null).ToList();

                List<New> result = new List<New>();

                foreach (var item in listNews)
                {
                    New temp = new New();
                    temp.Id = item.Id;
                    temp.TitleVn = item.TitleVn;
                    temp.TitleEn = item.TitleEn;
                    temp.DescriptionVn = item.DescriptionVn;
                    temp.DescriptionEn = item.DescriptionEn;
                    temp.DetailsVn = item.DetailsVn;
                    temp.DetailsEn = item.DetailsEn;
                    temp.CreateOn = item.CreateOn;
                    temp.Deleted = item.Deleted;
                    temp.Image = item.Image;
                    //temp.Avatar = item.Avatar;

                    result.Add(temp);
                }

                return result;
            }
            catch
            {
                return new List<New>();
            }
        }
    }
}